package com.bbcare.followup.plat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.bbcare.comm.AppException;
import com.bbcare.followup.plat.dto.bo.PatientDumpLogStatisticsBO;
import com.bbcare.followup.plat.dto.bo.PatientDumpTimesStatisticsBO;
import com.bbcare.followup.plat.dto.bo.PatientUseDrugStatisticsBO;
import com.bbcare.followup.plat.dto.request.PatientDumpStatisticsReqDTO;
import com.bbcare.followup.plat.dto.response.PatientDumpLogStatisticsResDTO;
import com.bbcare.followup.plat.dto.response.PatientDumpTimesStatisticsResDTO;
import com.bbcare.followup.plat.dto.response.PatientDumpUseDrugStatisticsResDTO;
import com.bbcare.followup.plat.service.IDumpChartService;
import com.bbcare.followup.plat.store.ibatis.IDumpBreastRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

@Service
public class DumpChartService implements IDumpChartService {

    @Autowired
    private IDumpBreastRecordDao dumpBreastRecordDao;

    @Override
    public List<PatientDumpLogStatisticsResDTO> patientLogStatistics(PatientDumpStatisticsReqDTO reqDTO) {
        if(StrUtil.isBlank(reqDTO.getPatientId()) || reqDTO.getType() == null){
           throw new AppException("1001","参数不能为空");
        }
        HashMap<String, Object> paramMap = Dict.parse(reqDTO);
        if(reqDTO.getType() == 1){
            //7天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-7),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }
        if(reqDTO.getType() == 2){
            //30天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-30),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }

        List<PatientDumpLogStatisticsBO> patientDumpLogStatisticsBOS = dumpBreastRecordDao.selectPatientLogStatistics(paramMap);
        //拉一个日期的范围
        List<DateTime> rangeToList = new ArrayList<>();
        if(reqDTO.getType() == 0){
            if(CollUtil.isEmpty(patientDumpLogStatisticsBOS)){
                return new ArrayList<>();
            }
            //找出最小的日期
            PatientDumpLogStatisticsBO minDay = patientDumpLogStatisticsBOS.stream().sorted( Comparator.comparing( PatientDumpLogStatisticsBO::getDays)).findFirst().get();
            if(minDay!=null){
                rangeToList = DateUtil.rangeToList(minDay.getDays(),DateUtil.date(), DateField.DAY_OF_YEAR);
            }
        }
        if(reqDTO.getType() == 1){
            rangeToList = DateUtil.rangeToList(DateUtil.offsetDay(DateUtil.date(),-6),DateUtil.date(), DateField.DAY_OF_YEAR);
        }
        if(reqDTO.getType() == 2){
            rangeToList = DateUtil.rangeToList(DateUtil.offsetDay(DateUtil.date(),-29),DateUtil.date(), DateField.DAY_OF_YEAR);
        }
        ConcurrentMap<String, PatientDumpLogStatisticsBO> patientDumpMap = patientDumpLogStatisticsBOS.stream().collect(Collectors.toConcurrentMap(e -> DateUtil.format(e.getDays(),"yyyy-MM-dd"), e -> e));
        List<PatientDumpLogStatisticsResDTO> resDTOS = rangeToList.stream().map(datetime->{
            String dateTime = DateUtil.format(datetime,"yyyy-MM-dd");
            PatientDumpLogStatisticsResDTO resDTO = new PatientDumpLogStatisticsResDTO();
            if(patientDumpMap.containsKey(dateTime)){
                PatientDumpLogStatisticsBO patientDumpLogStatisticsBO = patientDumpMap.get(dateTime);
                BeanUtil.copyProperties(patientDumpLogStatisticsBO,resDTO);
            }else{
                resDTO.setCount(0);
                resDTO.setQuantity(BigDecimal.ZERO);
            }
            resDTO.setDays(DateUtil.format(datetime,"MM-dd"));
            return resDTO;
        }).collect(Collectors.toList());
        return resDTOS;
    }

    @Override
    public List<PatientDumpTimesStatisticsResDTO> patientTimesStatistics(PatientDumpStatisticsReqDTO reqDTO) {
        if(StrUtil.isBlank(reqDTO.getPatientId()) || reqDTO.getType() == null){
            throw new AppException("1001","参数不能为空");
        }
        HashMap<String, Object> paramMap = Dict.parse(reqDTO);
        if(reqDTO.getType() == 1){
            //3天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-2),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }
        if(reqDTO.getType() == 2){
            //7天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-6),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }

        List<PatientDumpTimesStatisticsBO> patientDumpTimesStatisticsBOS = dumpBreastRecordDao.selectPatientTimesStatistics(paramMap);
        //拉一个日期的范围
        List<DateTime> rangeToList = new ArrayList<>();
        if(reqDTO.getType() == 0){
            if(CollUtil.isEmpty(patientDumpTimesStatisticsBOS)){
                return new ArrayList<>();
            }
            //找出最小的日期
            PatientDumpTimesStatisticsBO minDay = patientDumpTimesStatisticsBOS.stream().sorted( Comparator.comparing( PatientDumpTimesStatisticsBO::getDays)).findFirst().get();
            if(minDay!=null){
                rangeToList = DateUtil.rangeToList(minDay.getDays(),DateUtil.date(), DateField.DAY_OF_YEAR);
            }
        }
        if(reqDTO.getType() == 1){
            rangeToList = DateUtil.rangeToList(DateUtil.offsetDay(DateUtil.date(),-2),DateUtil.date(), DateField.DAY_OF_YEAR);
        }
        if(reqDTO.getType() == 2){
            rangeToList = DateUtil.rangeToList(DateUtil.offsetDay(DateUtil.date(),-6),DateUtil.date(), DateField.DAY_OF_YEAR);
        }
        ConcurrentMap<String, PatientDumpTimesStatisticsBO> patientDumpMap = patientDumpTimesStatisticsBOS.stream().collect(Collectors.toConcurrentMap(e -> DateUtil.format(e.getDays(),"yyyy-MM-dd"), e -> e));
        List<PatientDumpTimesStatisticsResDTO> resDTOS = rangeToList.stream().map(datetime->{
            String dateTime = DateUtil.format(datetime,"yyyy-MM-dd");
            PatientDumpTimesStatisticsResDTO resDTO = new PatientDumpTimesStatisticsResDTO();
            if(patientDumpMap.containsKey(dateTime)){
                PatientDumpTimesStatisticsBO patientDumpTimesStatisticsBO = patientDumpMap.get(dateTime);
                BeanUtil.copyProperties(patientDumpTimesStatisticsBO,resDTO);
            }else{
                resDTO.setLeftQuantity(BigDecimal.ZERO);
                resDTO.setLeftMinute(BigDecimal.ZERO);
                resDTO.setRightQuantity(BigDecimal.ZERO);
                resDTO.setRightMinute(BigDecimal.ZERO);
            }
            resDTO.setDays(DateUtil.format(datetime,"MM-dd"));
            return resDTO;
        }).collect(Collectors.toList());
        return resDTOS;
    }


    @Override
    public List<PatientDumpUseDrugStatisticsResDTO> patientUseDrugStatistics(PatientDumpStatisticsReqDTO reqDTO) {
        if(StrUtil.isBlank(reqDTO.getPatientId()) || reqDTO.getType() == null){
            throw new AppException("1001","参数不能为空");
        }
        HashMap<String, Object> paramMap = Dict.parse(reqDTO);
        if(reqDTO.getType() == 1){
            //3天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-2),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }
        if(reqDTO.getType() == 2){
            //7天
            paramMap.put("dumpTimeStart",DateUtil.parse(DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-6),"yyyy-MM-dd 00:00:00")));
            paramMap.put("dumpTimeEnd",DateUtil.parse(DateUtil.format(DateUtil.date(),"yyyy-MM-dd 23:59:59")));
        }
        List<PatientUseDrugStatisticsBO> patientUseDrugStatisticsBOS = dumpBreastRecordDao.selectPatientUseDrugStatistics(paramMap);
        List<PatientDumpUseDrugStatisticsResDTO> resDTOS = patientUseDrugStatisticsBOS.stream().map(patientUseDrugStatisticsBO->{
            PatientDumpUseDrugStatisticsResDTO resDTO = new PatientDumpUseDrugStatisticsResDTO();
            BeanUtil.copyProperties(patientUseDrugStatisticsBO,resDTO);
            resDTO.setDays(DateUtil.format(patientUseDrugStatisticsBO.getDays(),"yyyy-MM-dd"));
            return resDTO;
        }).collect(Collectors.toList());
        return resDTOS;
    }
}
